package cn.echo.modules.sys.controller.CategoryController;

import cn.echo.modules.sys.entity.Category;
import cn.echo.modules.sys.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("category")
public class CategoryController{
    @Autowired
    private CategoryService categoryService ;
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String list(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                       @RequestParam(required = false,defaultValue = "5") Integer pageSize,
                       Model model){
        PageHelper.startPage(pageNum,pageSize);
        Page<Category> categoryList = (Page) categoryService.findAll(new Category());
        PageInfo<Category> pageInfo = new PageInfo(categoryList);
        model.addAttribute("page",pageInfo);
        return "sys/category/Category";
    }
    @RequestMapping(value="toupdate",method = RequestMethod.GET)
    public String toUpdate(String id,Model model){
        Category category = categoryService.findById(new Category(id));
        model.addAttribute("category",category);
        return "sys/category/CateUpdate";
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(Category category){
        boolean flag = categoryService.update(category);
        return "redirect:list";
    }
    @RequestMapping(value = "toinsert",method = RequestMethod.GET)
    public String toinsert(String id,Model model){
        return "sys/category/Cateadd";
    }
    @RequestMapping(value = "insert" ,method = RequestMethod.POST)
    public  String insert(Category category){
      boolean add=categoryService.insert(category);
        return "redirect:list";
    }
    @RequestMapping(value = "delete",method = RequestMethod.GET)
     public String delete(Category category){
//        CategoryService categoryService=new CategoryService();
//        categoryService.deleteById(category);
        boolean isSuccess=categoryService.deleteById(category);
        return "redirect:list";
    }
    @RequestMapping(value="findAll",method = RequestMethod.GET)
    public String findAll(){
        return "sys/category/Cate";
    }
}
